/*
 * @Author: TAN7_Main 1240973173@qq.com
 * @Date: 2023-11-28 20:08:12
 * @LastEditors: TAN7_Main 1240973173@qq.com
 * @LastEditTime: 2024-05-29 07:51:23
 * @FilePath: \2023_super_caps\U5erC0de\tool\vofa.c
 * @Description: 这是默�?��?�置,请�?�置`customMade`, 打开koroFileHeader查看配置
 * 进�?��?�置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "vofa.h"
#include "stdarg.h"
#include "stdio.h"
#include "string.h"
#include "usart.h"
#include "stdint.h"

// UART_HandleTypeDef huart5;
// DMA_HandleTypeDef hdma_usart5_tx;
#define HOST_UART huart2
#define MAX_FLOATS_NUM 5 // 最大浮点数数量
#define buf_len ((MAX_FLOATS_NUM * (6 + 5 + 1) + 1))
// 发送精度最大小数点�?5�?+小数点后5�?

char buffer[buf_len] = {0}; // 选择足�?�大的缓冲区
uint16_t tx_data_len = 0;
struct
{
    float MAX_TX_FREQ;
    float MIN_TX_TICK;
    float REAL_TX_FREQ;
    long Buffer_Boom_cnt; // 专门用于判断Buffer有没有炸掉，并�?�录炸掉的�?�数
} VOFA_INFO = {0};
float dfloat[8] = {0};


int cac = 0;


void show_data_to_vofa(void)
{

    static uint32_t tickstart = 0;
    if (/*host_uart.gState == HAL_UART_STATE_READY &&
        HAL_GetTick() - tickstart > VOFA_INFO.MIN_TX_TICK +*/
        1)
    {
        VOFA_INFO.REAL_TX_FREQ = 1000.f / (HAL_GetTick() - tickstart);


		cac++;
        float Dfloats[MAX_FLOATS_NUM] = {
			(float)cac,
			(float)cac+1,
			(float)cac+2,
			(float)cac+3,
			(float)cac+4,
        };


        memcpy(dfloat, Dfloats, sizeof(Dfloats));
        char *buffer_ptr = buffer;

        tx_data_len = 0;
        for (char i = 0; i < MAX_FLOATS_NUM; i++)
        {
            tx_data_len +=
                snprintf(&buffer_ptr[tx_data_len], sizeof(buffer) - tx_data_len,
                         "%.3f,", Dfloats[i]);
        }
        // 添加 \r\n 并发送数�?
        if (tx_data_len > 0)
        {
            buffer[tx_data_len - 1] = '\n';
            HAL_UART_Transmit(&HOST_UART, (uint8_t *)buffer, tx_data_len, 0xfff);
            // HAL_UART_Transmit(&HOST_UART, (uint8_t *)buffer, tx_data_len);
        }
        if (tx_data_len > buf_len)
        {
            VOFA_INFO.Buffer_Boom_cnt++;
        }
        VOFA_INFO.MIN_TX_TICK =
            1000.f * tx_data_len * (1 + 8 + 0 + 1) / HOST_UART.Init.BaudRate;
        VOFA_INFO.MAX_TX_FREQ = 1000.f / VOFA_INFO.MIN_TX_TICK;
        tickstart = HAL_GetTick();
    }
}
